package com.yanqu.road.dao.impl.activity.fellingtrees;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.fellingtrees.FellingTreesEquip;
import com.yanqu.road.entity.activity.fellingtrees.FellingTreesSetting;
import com.yanqu.road.entity.activity.fellingtrees.FellingTreesUserData;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FellingTreesUserDataDaoImpl extends TempDataObjectDao<FellingTreesUserData> {

    public static String getFellingTreesUserDataSql = "select * from `t_u_felling_trees_user` where `activityId`=? and `userId`=?";

    public FellingTreesUserData getFellingTreesUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getFellingTreesUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateFellingTreesUserDataSql = "update `t_u_felling_trees_user` set gold=?,attack=?,topLevel=?,equipPosArr=?,equipMap=?,items=?,maxUid=?,buffMap=?,energy=?,recoveryTime=?,rabbitEnergy=?,rabbitRecoveryTime=?,rabbitGuide=?,lastLevel=?,lastHp=?,setting=?,specialEquipGuide=?,daoqiGuide=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(FellingTreesUserData fellingTreesUserData) {
        fellingTreesUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesUserData.getGold().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesUserData.getAttack().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getTopLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getEquipPosArr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getEquipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(fellingTreesUserData.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getMaxUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getRabbitEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getRabbitRecoveryTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, fellingTreesUserData.isRabbitGuide()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getLastLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesUserData.getLastHp().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getSetting())));
        para.put(i++, new DbParameter(Types.BOOLEAN, fellingTreesUserData.isSpecialEquipGuide()));
        para.put(i++, new DbParameter(Types.BOOLEAN, fellingTreesUserData.isDaoqiGuide()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getUserId()));
        if (getDb().executeNoneQuery(updateFellingTreesUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertFellingTreesUserDataSql = "insert into `t_u_felling_trees_user`(activityId,userId,gold,attack,topLevel,equipPosArr,equipMap,items,maxUid,buffMap,energy,recoveryTime,rabbitEnergy,rabbitRecoveryTime,rabbitGuide,lastLevel,lastHp,setting,specialEquipGuide,daoqiGuide) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(FellingTreesUserData fellingTreesUserData) {
        fellingTreesUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesUserData.getGold().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesUserData.getAttack().toString()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getTopLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getEquipPosArr())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getEquipMap())));
        para.put(i++, new DbParameter(Types.VARCHAR, PropertyHelper.parsePropertyToString(fellingTreesUserData.getItems())));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getMaxUid()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getRecoveryTime()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getRabbitEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, fellingTreesUserData.getRabbitRecoveryTime()));
        para.put(i++, new DbParameter(Types.BOOLEAN, fellingTreesUserData.isRabbitGuide()));
        para.put(i++, new DbParameter(Types.INTEGER, fellingTreesUserData.getLastLevel()));
        para.put(i++, new DbParameter(Types.VARCHAR, fellingTreesUserData.getLastHp().toString()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(fellingTreesUserData.getSetting())));
        para.put(i++, new DbParameter(Types.BOOLEAN, fellingTreesUserData.isSpecialEquipGuide()));
        para.put(i++, new DbParameter(Types.BOOLEAN, fellingTreesUserData.isDaoqiGuide()));
        if (getDb().executeNoneQuery(insertFellingTreesUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected FellingTreesUserData parseData(ResultSet rs) throws SQLException {
        FellingTreesUserData fellingTreesUserData = new FellingTreesUserData();
        fellingTreesUserData.setActivityId(rs.getInt("activityId"));
        fellingTreesUserData.setUserId(rs.getLong("userId"));
        fellingTreesUserData.setGold(new BigInteger(rs.getString("gold")));
        fellingTreesUserData.setAttack(new BigInteger(rs.getString("attack")));
        fellingTreesUserData.setTopLevel(rs.getInt("topLevel"));
        fellingTreesUserData.setEquipPosArr(JSON.parseObject(rs.getString("equipPosArr"), long[].class));
        fellingTreesUserData.setEquipMap(JSON.parseObject(rs.getString("equipMap"), new TypeReference<ConcurrentHashMap<Long, FellingTreesEquip>>() {
        }));
        fellingTreesUserData.setItems(PropertyHelper.parseStringToProperty(rs.getString("items")));
        fellingTreesUserData.setMaxUid(rs.getLong("maxUid"));
        fellingTreesUserData.setBuffMap(JSON.parseObject(rs.getString("buffMap"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {
        }));
        fellingTreesUserData.setEnergy(rs.getInt("energy"));
        fellingTreesUserData.setRecoveryTime(rs.getLong("recoveryTime"));
        fellingTreesUserData.setRabbitEnergy(rs.getInt("rabbitEnergy"));
        fellingTreesUserData.setRabbitRecoveryTime(rs.getLong("rabbitRecoveryTime"));
        fellingTreesUserData.setRabbitGuide(rs.getBoolean("rabbitGuide"));
        fellingTreesUserData.setLastLevel(rs.getInt("lastLevel"));
        fellingTreesUserData.setLastHp(new BigInteger(rs.getString("lastHp")));
        fellingTreesUserData.setSetting(JSON.parseObject(rs.getString("setting"), FellingTreesSetting.class));
        fellingTreesUserData.setSpecialEquipGuide(rs.getBoolean("specialEquipGuide"));
        fellingTreesUserData.setDaoqiGuide(rs.getBoolean("daoqiGuide"));
        return fellingTreesUserData;
    }


}
